﻿using Common.Contract.Customer.Record;
using Common.Contract.Customer.Request;
using Common.Contract.Customer.Response;
using Common.Contract.Paging;
using Common.Service.Command;
using Customer.Model.Interface;
using Customer.Model.Model;
using System;
using System.Collections.Generic;
using System.Linq;

namespace Customer.Core.ServiceCommand
{
    public class RetrieveCustomerI2bDetailByCifServiceCommand : ProviderServiceCommand<IProviderFactory, RetrieveCustomerI2bDetailByCifRequest, RetrieveCustomerI2bDetailByCifResponse>
    {
        #region Properties
        public string Cif;
        public int pageSize = 10, pageIndex = 0, totalRecord = 0;
        #endregion
        public RetrieveCustomerI2bDetailByCifServiceCommand(IProviderFactory providers) : base(providers) { }
        protected override void Validate(RetrieveCustomerI2bDetailByCifRequest request)
        {
            if (request == null) throw new ArgumentNullException("request");
            if (!request.Header.ApplicationKey.HasValue || request.Header.ApplicationKey == Guid.Empty)
            {
                throw new ArgumentException("ApplicationKey must be set", "request");
            }
            if (string.IsNullOrEmpty(request.Cif))
            {
                throw new ArgumentException("Cif customer must be set", "request");
            }
            else
            {
                Cif = Request.Cif;
            }
            if (Request.Page != null)
            {
                pageIndex = Request.Page.PageIndex ?? 0;
                pageSize = Request.Page.PageSize ?? 0;
            }
        }

        protected override bool OnExecute(RetrieveCustomerI2bDetailByCifResponse response)
        {
            bool isSuccess = true;
            string errorMessage = string.Empty;
            List<CustomerI2BDetail> records = new List<CustomerI2BDetail>();
            List<ProductI2BDetail> listDetail = Providers.Customers.RetrieveListProductI2BDetailByCif(out totalRecord, Cif, pageSize, pageIndex);
            if (listDetail != null && listDetail.Count > 0)
            {
                records = listDetail.Select(x => new CustomerI2BDetail
                {
                    UserName = x.UserName,
                    Status = x.AmndStatus
                }).ToList();
            }
            response.Records = records;
            response.Page = new PageRecord
            {
                PageIndex = pageIndex,
                PageSize = pageSize,
                TotalRecords = totalRecord
            };
            return isSuccess;
        }


    }
}
